package com.quectel.util.common;

import cn.hutool.crypto.digest.MD5;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

/**
 * map的工具类不提供传输使用
 */
public class MapUtils {


    @Getter
    private final Map<Object, Object> initMap;


    public MapUtils() {
        this.initMap = new LinkedHashMap<>();
    }

    public MapUtils(Map<Object, Object> initMap) {
        this.initMap = initMap;
    }

    public static MapUtils getInstance() {
        return new MapUtils();
    }

    public static boolean allValueIsEmpty(Map<String, Object> params) {
        if (params == null || params.size() <= 0) {
            return true;
        }
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof String) {
                if (StringUtils.isBlank((String) value)) {
                    continue;
                }
            }
            if (value != null) {
                return false;
            }
        }
        return true;
    }

    public static boolean allStrValueIsEmpty(Map<String, Object> params) {
        if (params == null || params.size() <= 0) {
            return true;
        }
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (StringUtils.isNotEmpty((CharSequence) entry.getValue())) {
                return false;
            }
        }
        return true;
    }

    public Boolean isNotNullAndNotEmpty(String... keys) {
        if (keys == null) {
            return false;
        }
        for (String key : keys) {
            if (!this.initMap.containsKey(key)) {
                return false;
            }
            if (this.initMap.get(key) == null) {
                return false;
            }
            if (this.initMap.get(key).toString().trim().length() == 0) {
                return false;
            }
        }
        return true;
    }

    public Boolean isNullOrEmpty(String... keys) {
        return !isNotNullAndNotEmpty(keys);
    }


    /**
     * 根据key得到value
     *
     * @param keys
     * @return
     */
    public Object[] getValues(String... keys) {
        if (keys == null) {
            return null;
        }
        Object[] values = new Object[keys.length];
        for (int i = 0; i < keys.length; i++) {
            values[i] = this.initMap.get(keys);
        }

        return values;


    }


    public String genUUID() {

        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public void like(Object key) {
        if (this.initMap.containsKey(key)) {
            this.initMap.put(key, "%" + this.initMap.get(key).toString() + "%");
        }
    }

    public String MD5(Object key) {
        if (this.initMap.containsKey(key)) {
            return MD5.create().digestHex(this.initMap.get(key).toString());
        }
        return null;
    }

    public String getString(Object key) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        return this.initMap.get(key).toString();

    }

    public String[] getValues(Object key) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        return (String[]) this.initMap.get(key);

    }

    public Short getShort(Object key) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        return Short.parseShort(this.getString(key));
    }

    public Integer getInt(Object key) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        return Integer.parseInt(this.getString(key));
    }

    public Long getLong(Object key) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        return Long.parseLong(this.getString(key));
    }

    public Double getDouble(Object key) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        return Double.parseDouble(this.getString(key));
    }

    public Float getFloat(Object key) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        return Float.parseFloat(this.getString(key));
    }

    public BigDecimal getBigDecimal(Object key) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        return new BigDecimal(this.getString(key));
    }

    /**
     * 精确计算  四舍五入
     * number 小数点 几位
     * targetObj 目标对象
     */
    public BigDecimal getBigDecimal(Object key, Integer number) {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        if (number == null || number < 0) {
            number = 2;
        }
        return getBigDecimal(key).setScale(number, BigDecimal.ROUND_HALF_UP); //四舍五入
    }

    public Boolean getBoolean(Object key) {
        if (!this.initMap.containsKey(key)) {
            return false;
        }
        if (this.initMap.get(key) == null) {
            return false;
        }
        return Boolean.parseBoolean(this.getString(key));
    }

    /**
     * 得到日期
     *
     * @param key
     * @param format 可以传null("yyyy-MM-dd HH:mm:ss")  默认为yyyy-MM-dd
     * @return
     * @throws ParseException
     */
    public Date getDate(String key, String format) throws ParseException {
        if (!this.initMap.containsKey(key)) {
            return null;
        }
        if (this.initMap.get(key) == null) {
            return null;
        }
        if (format == null) {
            format = new String("yyyy-MM-dd");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(this.initMap.get(key).toString());
        return date;
    }

    /**
     * 得到日期字符串
     *
     * @param format 可以传null("yyyy-MM-dd HH:mm:ss")  默认为yyyy-MM-dd HH:mm:ss
     * @return
     * @throws ParseException
     */
    public static String getCurrentDate(String format) {
        if (format == null) {
            format = new String("yyyy-MM-dd HH:mm:ss");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }


}
